package com.zyy.smartcity1129.movie;

import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.VideoView;

import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.gson.JsonObject;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.MovieContextComment;
import com.zyy.smartcity1129.data.MovieContextFilm;
import com.zyy.smartcity1129.data.ResultInfo;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import okhttp3.RequestBody;

/**
 * @ClassName: MovieContextFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 21/11/2021$
 * @Version: 1.0
 */
public class MovieContextFragment extends BaseFragment {
    private String movieName = MovieMovieFragment.movieName;
    private int movieId = MovieMovieFragment.movieId;
    private ImageView imgMovieContext1;
    private TextView tvMovieContext1;
    private TextView tvMovieContext2;
    private TextView tvMovieContext3;
    private LinearLayout line1;
    private LinearLayout line2;
    private TextView tvMovieContext4;
    private TextView tvMovieContext5;
    private WebView webViewMovieContext;
    private TextView textView19;
    private TextView textView20;
    private VideoView videoViewMovieContext;
    private ImageView imgMovieContext2;
    private TextView tvMovieContext21;
    private RecyclerView rvMovieContext;
    private LinearLayout lineMovieContext1;
    private LinearLayout lineMovieContext2;
    private LinearLayout lineMovieContext4;
    ExecutorService executor = Executors.newFixedThreadPool(2);
    MutableLiveData<AtomicInteger> mData = new MutableLiveData<>();
    AtomicInteger mInteger = new AtomicInteger(0);
    List<MovieContextComment.RowsBean> commentList = new ArrayList<>();
    MovieContextFilm.DataBean dataBean;
    RvAd<MovieContextComment.RowsBean> rvAd;
    private EditText edit, edit10;
    private Button btn;

    @Override
    protected int layId() {
        return R.layout.fragment_movie_context;
    }

    @Override
    protected String title() {
        return movieName;
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    @Override
    protected void init(View v) {
        initView(v);
        executor.execute(new Runnable() {
            @Override
            public void run() {
                dataBean = Http.getInstance().get("/prod-api/api/movie/film/detail/" + movieId, null, MovieContextFilm.class, 1).getData();
                mInteger.getAndIncrement();
                mData.postValue(mInteger);
            }
        });
        executor.execute(new Runnable() {
            @Override
            public void run() {
                commentList = Http.getInstance().get("/prod-api/api/movie/film/comment/list", null, MovieContextComment.class, 1).getRows();
                mInteger.getAndIncrement();
                mData.postValue(mInteger);
            }
        });
        mData.observe(this, atomicInteger -> {
            if (mInteger.get() == 2) {
                List<MovieContextComment.RowsBean> comList = new ArrayList<>();
                comList.addAll(commentList.stream().filter(a -> a.getMovieId() == movieId).collect(Collectors.toList()));
                Glide.with(imgMovieContext1.getContext()).load(Http.base + dataBean.getCover()).into(imgMovieContext1);
                tvMovieContext1.setText(dataBean.getName());
                tvMovieContext2.setText(dataBean.getLanguage());
                tvMovieContext3.setText(dataBean.getPlayDate() + "中国大陆上映/" + dataBean.getDuration() + "分钟");
                int score = dataBean.getScore();
                if (score > 6) {
                    tvMovieContext4.setTextColor(getResources().getColor(R.color.sell));
                } else {
                    tvMovieContext4.setTextColor(getResources().getColor(R.color.yellow));
                }
                tvMovieContext4.setText(String.valueOf(dataBean.getScore()));
                tvMovieContext5.setText(dataBean.getLikeNum() + "人想看 " + dataBean.getFavoriteNum() + "人看过");
                webViewMovieContext.loadDataWithBaseURL(Http.base, dataBean.getIntroduction(), "text/html", "utf-8", null);
                initAdapter(comList);
                rvMovieContext.setLayoutManager(new LinearLayoutManager(getContext()));
                rvMovieContext.setAdapter(rvAd);
            }
        });
        line1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        JsonObject jsonObject = new JsonObject();
                        String result = Http.getInstance().get("/prod-api/api/movie/film/like/" + movieId, RequestBody.create(Http.m, jsonObject.toString()), ResultInfo.class, 2).getMsg();
                        dataBean = Http.getInstance().get("/prod-api/api/movie/film/detail/" + movieId, null, MovieContextFilm.class, 1).getData();
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tvMovieContext5.setText(dataBean.getLikeNum() + "人想看 " + dataBean.getFavoriteNum() + "人看过");
                            }
                        });
                    }
                }).start();
            }
        });
        line2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        JsonObject jsonObject = new JsonObject();
                        String result = Http.getInstance().get("/prod-api/api/movie/film/favorite/" + movieId, RequestBody.create(Http.m, jsonObject.toString()), ResultInfo.class, 2).getMsg();
                        dataBean = Http.getInstance().get("/prod-api/api/movie/film/detail/" + movieId, null, MovieContextFilm.class, 1).getData();
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tvMovieContext5.setText(dataBean.getLikeNum() + "人想看 " + dataBean.getFavoriteNum() + "人看过");
                            }
                        });
                    }
                }).start();
            }
        });
        tvMovieContext21.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lineMovieContext4.setVisibility(View.VISIBLE);
            }
        });
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = edit.getText().toString().trim();
                String score = edit10.getText().toString().trim();
                if (!content.equals("")) {
                    lineMovieContext4.setVisibility(View.GONE);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            JsonObject jsonObject=new JsonObject();
                            jsonObject.addProperty("movieId",movieId);
                            jsonObject.addProperty("content",content);
//                            jsonObject.addProperty("score", score);
                            String result = Http.getInstance().get("/prod-api/api/movie/film/comment", RequestBody.create(Http.m, jsonObject.toString()), ResultInfo.class,2).getMsg();
                            commentList = Http.getInstance().get("/prod-api/api/movie/film/comment/list", null, MovieContextComment.class, 1).getRows();
                            List<MovieContextComment.RowsBean> comList = new ArrayList<>();
                            comList.addAll(commentList.stream().filter(a -> a.getMovieId() == movieId).collect(Collectors.toList()));
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    initAdapter(comList);
                                    rvMovieContext.setAdapter(rvAd);
                                }
                            });
                        }
                    }).start();
                }
            }
        });
    }

    private void initAdapter(List<MovieContextComment.RowsBean> comList) {
        rvAd = new RvAd<MovieContextComment.RowsBean>(comList, R.layout.rv_moviecomment) {
            private TextView tv1;
            private WebView web2;
            private TextView tv3;
            private TextView tv4;
            private LinearLayout rv_line;

            private void initView(View v) {
                tv1 = (TextView) v.findViewById(R.id.tv1);
                web2 = (WebView) v.findViewById(R.id.web2);
                tv3 = (TextView) v.findViewById(R.id.tv3);
                tv4 = (TextView) v.findViewById(R.id.tv4);
                rv_line = (LinearLayout) v.findViewById(R.id.rv_linecomment);
            }

            @Override
            protected void initR(View iv, MovieContextComment.RowsBean rowsBean, int p) {
                initView(iv);
                tv1.setText(rowsBean.getNickName());
                web2.loadDataWithBaseURL(Http.base, rowsBean.getContent(), "text/html", "utf-8", null);
                tv3.setText(rowsBean.getCommentDate());
                tv4.setText(String.valueOf(rowsBean.getLikeNum()));
                rv_line.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                JsonObject jsonObject = new JsonObject();
                                String result = Http.getInstance().get("/prod-api/api/movie/press/pressComment/like/" + rowsBean.getId(), RequestBody.create(Http.m, jsonObject.toString()), ResultInfo.class, 3).getMsg();
                                commentList = Http.getInstance().get("/prod-api/api/movie/film/comment/list", null, MovieContextComment.class, 1).getRows();
                                List<MovieContextComment.RowsBean> comList = new ArrayList<>();
                                comList.addAll(commentList.stream().filter(a -> a.getMovieId() == movieId).collect(Collectors.toList()));
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        tv4.setText(String.valueOf(comList.get(p).getLikeNum()));
                                    }
                                });
                            }
                        }).start();


                    }
                });
            }
        };
    }

    private void initView(View v) {
        imgMovieContext1 = (ImageView) v.findViewById(R.id.imgMovieContext1);
        tvMovieContext1 = (TextView) v.findViewById(R.id.tvMovieContext1);
        tvMovieContext2 = (TextView) v.findViewById(R.id.tvMovieContext2);
        tvMovieContext3 = (TextView) v.findViewById(R.id.tvMovieContext3);
        line1 = (LinearLayout) v.findViewById(R.id.line1);
        line2 = (LinearLayout) v.findViewById(R.id.line2);
        tvMovieContext4 = (TextView) v.findViewById(R.id.tvMovieContext4);
        tvMovieContext5 = (TextView) v.findViewById(R.id.tvMovieContext5);
        webViewMovieContext = (WebView) v.findViewById(R.id.webViewMovieContext);
        textView19 = (TextView) v.findViewById(R.id.textView19);
        textView20 = (TextView) v.findViewById(R.id.textView20);
        videoViewMovieContext = (VideoView) v.findViewById(R.id.videoViewMovieContext);
        imgMovieContext2 = (ImageView) v.findViewById(R.id.imgMovieContext2);
        tvMovieContext21 = (TextView) v.findViewById(R.id.tvMovieContext21);
        rvMovieContext = (RecyclerView) v.findViewById(R.id.rvMovieContext);
        lineMovieContext1 = (LinearLayout) v.findViewById(R.id.lineMovieContext1);
        lineMovieContext2 = (LinearLayout) v.findViewById(R.id.lineMovieContext2);
        lineMovieContext4 = (LinearLayout) v.findViewById(R.id.lineMoveContext5);
        edit = (EditText) v.findViewById(R.id.edit);
        edit10 = (EditText) v.findViewById(R.id.edit10);
        btn = (Button) v.findViewById(R.id.btn);
    }
}
